package com.xkdx.caipiao;

import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;

import com.xkdx.caipiao.application.MyApplication;
import com.xkdx.caipiao.listener.OnAlertDialogOkListener;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.wiget.MyAlertDialog;

public class MainTabActivity extends TabActivity implements
		OnCheckedChangeListener, OnAlertDialogOkListener {
	private TabHost mHost;

	private Intent mLotteryInfoIntent;
	private Intent mLotteryHallIntent;
	private Intent mSetupIntent;
	private Intent mMyLotteryIntent;

	RadioButton lotteryHallButton;

	int backNumber = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_tables_caipiao);

		this.mLotteryInfoIntent = new Intent(this, LotteryInfoActivity.class);
		this.mLotteryHallIntent = new Intent(this, LotteryHallActivity.class);
		this.mSetupIntent = new Intent(this, SetupActivity.class);
		this.mMyLotteryIntent = new Intent(this, MyLotterActivity.class);
		setupIntent();
		initRadios();

	}

	@Override
	protected void onResume() {
		super.onResume();
		if(MyApplication.getInstance().isLaunchedNotification){
			//回到前台，设置到兑奖页面
			if (MyApplication.getInstance().isPushLunch) {
				MyApplication.getInstance().isPushLunch = false;
				MyApplication.getInstance().isLaunchedNotification = false;
				((RadioButton) findViewById(R.id.radio_button1))
						.setChecked(true);
				this.mHost.setCurrentTabByTag("message_tab");
				NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
				nm.cancel(1111);
			}

		}

	}
	

	private void initRadios() {
		lotteryHallButton = ((RadioButton) findViewById(R.id.radio_button0));
		lotteryHallButton.setOnCheckedChangeListener(this);
		lotteryHallButton.setChecked(true);

		((RadioButton) findViewById(R.id.radio_button1))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button2))
				.setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button3))
				.setOnCheckedChangeListener(this);

	}

	@SuppressLint("NewApi")
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (isChecked) {
			switch (buttonView.getId()) {
			case R.id.radio_button0:
				IConstants.FromRadioButton = 0;
				this.mHost.setCurrentTabByTag("mblog_tab");
				break;
			case R.id.radio_button1:
				IConstants.FromRadioButton = 0;
				this.mHost.setCurrentTabByTag("message_tab");
				break;
			case R.id.radio_button2:
				IConstants.FromRadioButton = 1;
				this.mHost.setCurrentTabByTag("userinfo_tab");
				break;
			case R.id.radio_button3:
				IConstants.FromRadioButton = 0;
				this.mHost.setCurrentTabByTag("setup_tab");
				break;
			}
		}
	}

	private void setupIntent() {
		this.mHost = getTabHost();
		TabHost localTabHost = mHost;

		if (localTabHost != null) {

			localTabHost.addTab(buildTabSpec("mblog_tab",
					R.string.lottery_hall, R.drawable.lottery_hall_selected_caipiao,
					mLotteryHallIntent));

			localTabHost.addTab(buildTabSpec("message_tab",
					R.string.lottery_info, R.drawable.lottery_info_no_selected_caipiao,
					mLotteryInfoIntent));

			localTabHost.addTab(buildTabSpec("userinfo_tab",
					R.string.lottery_info, R.drawable.my_lottery_no_selected_caipiao,
					mMyLotteryIntent));

			localTabHost.addTab(buildTabSpec("setup_tab", R.string.setup,
					R.drawable.setup_no_selected_caipiao, mSetupIntent));

		}
		this.mHost.setCurrentTabByTag("mblog_tab");

	}

	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,
			final Intent content) {
		return this.mHost
				.newTabSpec(tag)
				.setIndicator(getString(resLabel),
						getResources().getDrawable(resIcon))
				.setContent(content);
	}

	private long exitTime = 0;

	

	@Override
	public void handleOkClick() {

		System.exit(0);
		android.os.Process.killProcess(android.os.Process.myPid());
	}

	@Override
	public void handleDismissClick() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
		if (getIntent() != null) {
			if (getIntent().getStringExtra("tag") != null
					&& getIntent().getStringExtra("tag").equals("order")) {
				((RadioButton) findViewById(R.id.radio_button2))
						.setChecked(true);
				IConstants.FromRadioButton = 1;
				this.mHost.setCurrentTabByTag("userinfo_tab");
			}
			else if (getIntent().getStringExtra("tag") != null
					&& getIntent().getStringExtra("tag").equals("hall")) {
				((RadioButton) findViewById(R.id.radio_button0))
						.setChecked(true);
				this.mHost.setCurrentTabByTag("mblog_tab");
				
			}else if (getIntent().getStringExtra("tag") != null
					&& getIntent().getStringExtra("tag").equals("setup")) {
				((RadioButton) findViewById(R.id.radio_button2))
						.setChecked(true);
				IConstants.FromRadioButton = 1;
				this.mHost.setCurrentTabByTag("userinfo_tab");
				
			}

		}
		super.onNewIntent(intent);
	}

}